/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2019 Code Technology Studio
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.mxpio.webapp.entity;

import cn.hutool.core.util.CharsetUtil;
import com.mxpioframework.jpa.annotation.Generator;
import com.mxpioframework.security.entity.BaseEntity;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.nio.charset.Charset;
import java.util.concurrent.TimeUnit;

/**
 * ssh 信息
 */
@Entity
@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@Table(name = "MI_SSH_INFO")
@Schema(description = "SSH信息表")
public class MonitorSsh extends BaseEntity {
    @Id
    @Generator
    @Schema(description = "id")
    @Column(name = "id", length = 64)
    private String id;

    @Schema(description = "名称")
    @Column(name = "NAME")
    private String name;

    @Schema(description = "Host")
    @Column(name = "HOST")
    private String host;

    @Schema(description = "Port")
    @Column(name = "PORT")
    private Integer port;

    @Schema(description = "用户名")
    @Column(name = "USER")
    private String user;

    @Schema(description = "主机名（来源）")
    @Column(name = "PASSWORD")
    private String password;

    @Schema(description = "编码格式")
    @Column(name = "CHARSET")
    private String charset;

    @Schema(description = "文件目录")
    @Column(name = "HOSTNAME_SOURCE")
    private String fileDirs;

    @Schema(description = "节点超时时间")
    @Column(name = "TIMEOUT")
    private Integer timeout;

    /**
     * 超时时间
     *
     * @return 最小值 1 分钟
     */
    public int timeout() {
        if (this.timeout == null) {
            return (int) TimeUnit.SECONDS.toMillis(5);
        }
        return (int) TimeUnit.SECONDS.toMillis(Math.max(1, this.timeout));
    }

    public Charset charset() {
        return CharsetUtil.parse(this.getCharset(), CharsetUtil.CHARSET_UTF_8);
    }

}
